package com.woniu.p2p.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.*;
import com.woniu.p2p.mapper.*;
import com.woniu.p2p.service.InvestDetailsService;
import com.woniu.p2p.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class InvestDetailsServiceImpl implements InvestDetailsService {

    @Autowired
    InvestMapper investMapper;
    @Autowired
    InvestCommentMapper investCommentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInvestMapper userInvestMapper;
    @Autowired
    InvestStafguardMapper investStafguardMapper;
    @Autowired
    InvestRepayMapper investRepayMapper;
    @Autowired
    InvestCompanyMapper investCompanyMapper;
    @Autowired
    InvestRiskMapper investRiskMapper;
    @Autowired
    InvestAfterloanMapper investAfterloanMapper;
    @Autowired
    CompanyMincompanyMapper companyMincompanyMapper;
    @Autowired
    AfterloanReimbMapper afterloanReimbMapper;
    @Autowired
    UserZongMapper userZongMapper;



    @Override
    public InvestDetailsVo findAllById(Integer id) {
        Invest invest = investMapper.selectByPrimaryKey(id);
        Integer stafId = invest.getStafId();
        InvestStafguard investStafguard = investStafguardMapper.selectByPrimaryKey(stafId);
        Integer repayId = invest.getRepayId();
        InvestRepay investRepay = investRepayMapper.selectByPrimaryKey(repayId);
        Integer companyId = invest.getCompanyId();
        InvestCompany investCompany = investCompanyMapper.selectByPrimaryKey(companyId);
        Integer riskId = invest.getRiskId();
        InvestRisk investRisk = investRiskMapper.selectByPrimaryKey(riskId);
        Integer afterloanId = invest.getAfterloanId();
        InvestAfterloan investAfterloan = investAfterloanMapper.selectByPrimaryKey(afterloanId);

        List<UserInvest> allUserInvest = userInvestMapper.findAllUserInvest(id);

        InvestDetailsVo allInvestById = investMapper.findAllInvestById(id,investStafguard,investRepay,investCompany,investRisk,
                investAfterloan);
        allInvestById.setInvestNowMoney(allInvestById.getInvestAmount()-allInvestById.getInvesBalance());
        return allInvestById;
    }

    @Override
    public int InsertComment(InvestComment investComment) {
        return investCommentMapper.insert(investComment);
    }

    @Override
    public User findAllUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<InvestDetailsCommentsVo> findAllCommentById(Integer id,Integer start,Integer size) {
        PageHelper.startPage(start,size);
        List<InvestDetailsCommentsVo> allCommentById = investCommentMapper.findAllCommentById(id);
        PageInfo<InvestDetailsCommentsVo> pageInfo = new PageInfo<>(allCommentById);
        return pageInfo;
    }
    @Override
    @Transactional
    public int insertUserInvest(long dealmoney, double subribe, Date date, Integer userId, Integer InvestId,long money) {
        //修改用户的余额
        User user = userMapper.selectByPrimaryKey(userId);
        int i = userMapper.reduceUserBalance(money, userId);
        //增加用户跟投资表
        Invest invest = investMapper.selectByPrimaryKey(InvestId);
        String investDeadline = invest.getInvestDeadline();
        int i1 = Integer.parseInt(investDeadline);
        long v = (long) (money * i1 * 0.8);
        UserInvest userInvest = new UserInvest(userId, InvestId, money, date, "手动投标", v);
        int insert = userInvestMapper.insert(userInvest);
        //修改投资表
        int i2 = investMapper.updateInvestBalanceAndSubribe(InvestId, dealmoney, subribe + "");
        //增加总记录表
        long v1 = (long) (money * 0.0003);
        UserZong userZong = new UserZong(userId, date, "投资", money, v1);
        int insert1 = userZongMapper.insert(userZong);
        return 1;
    }


    @Override
    public PageInfo<InvestDetailsReimbVo> findAllInvestDetailsReimbVoBuInvestId(Integer id,Integer start,Integer num) {
        PageHelper.startPage(start,num);
        Invest invest = investMapper.selectByPrimaryKey(id);
        Integer afterloanId = invest.getAfterloanId();
        List<InvestDetailsReimbVo> allAfterloansReimbByAfterloanId = afterloanReimbMapper.findAllAfterloansReimbByAfterloanId(afterloanId);
        PageInfo<InvestDetailsReimbVo> pageInfo = new PageInfo<>(allAfterloansReimbByAfterloanId);
        return pageInfo;
    }

    @Override
    public PageInfo<InvestDetailsCompanyVo> findAllInvestDetailsCompanyVoByInvestId(Integer id,Integer start,Integer num) {
        PageHelper.startPage(start,num);
        Invest invest = investMapper.selectByPrimaryKey(id);
        Integer companyId = invest.getCompanyId();
        List<InvestDetailsCompanyVo> allMinCompany = companyMincompanyMapper.findAllMinCompany(companyId);
        PageInfo<InvestDetailsCompanyVo> pageInfo = new PageInfo<>(allMinCompany);
        return pageInfo;
    }

    @Override
    public PageInfo<UserInvest> findAllUserTou(Integer start,Integer num,Integer id) {
        PageHelper.startPage(start,num);
        List<UserInvest> list = userInvestMapper.findAllUserInvestById(id);
        PageInfo<UserInvest> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
}
